package com.ruoyi.common.utils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Description: 集合工具类
 * date: 2021/12/27 11:36
 *
 * @author 18305
 */
public final class CollectionUtils {

    private CollectionUtils() {
    }

    /**
     * @param data:      要分组的集合(M)
     * @param pieceSize: 每组的大小(N)
     * @return java.util.List<java.util.List < T>>
     * @Description M个元素的集合每N个分成一组
     * @author 18305
     * @Datetime 2021/12/27 10:36
     */
    public static <T> List<List<T>> groupOnPieceSize(Collection<T> data, int pieceSize) {
        if (pieceSize <= 0) {
            throw new IllegalArgumentException("参数错误");
        }
        if (StringUtils.isEmpty(data)) {
            return new ArrayList<>(0);
        }
        List<List<T>> resultList = new ArrayList<>(data.size());
        for (int i = 0; i < data.size(); i += pieceSize) {
            resultList.add(data.stream().skip(i).limit(pieceSize).collect(Collectors.toList()));
        }
        return resultList;
    }

    /**
     * @description : 返回当前月份开始一年的月份集合
     * @param:
     * @return : java.util.List<java.lang.String>
     * @author : kenn
     * @date : 2022/7/25 14:20
     */
    public static List<String> monthListOneYear() {
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH) + 1;
        List<String> monthList = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            monthList.add(month > 9 ? month + "" : "0" + month);
            if (month-- == 1) {
                month = 12;
            }
        }
        Collections.reverse(monthList);
        return monthList;
    }

    /**
     * @Description 返回时间段之间的月份集合
     * @param startDate: 开始时间
     * @param endDate: 结束时间
     * @return java.util.List<java.lang.String>
     * @author  18305
     * @Datetime  2022/1/26 16:49
     */
    public static List<String> monthListBetweenDate(String startDate,String endDate) {
        if (StringUtils.isEmpty(startDate) || StringUtils.isEmpty(endDate)) {
            return new ArrayList<>();
        }
        int startYear = Integer.parseInt(startDate.substring(0,startDate.indexOf("-")));
        int endYear = Integer.parseInt(endDate.substring(0,endDate.indexOf("-")));
        int startMonth = Integer.parseInt(startDate.substring(startDate.indexOf("-") + 1,startDate.lastIndexOf("-")));
        int endMonth= Integer.parseInt(endDate.substring(endDate.indexOf("-") + 1,endDate.lastIndexOf("-")));
        //两个时间相差的月份
        int between;
        if (startYear == endYear) {
            between = endMonth - startMonth + 1;
        } else {
            between = 13 - startMonth + endMonth;
        }
        List<String> monthList = new ArrayList<>();
        for (int i = 0; i < between; i++) {
            monthList.add(startMonth > 9 ? startMonth + "" : "0" + startMonth);
            if (startMonth++ == 12) {
                startMonth = 1;
            }
        }
        return monthList;
    }


    public static List<Double> fullRatePercent(List<BigDecimal> infoList, int scale) {
        BigDecimal totalDecimal = infoList.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
        List<Double> resultList = new ArrayList<>(infoList.size());
        BigDecimal totalRate = BigDecimal.ZERO;
        if (totalDecimal.doubleValue() > 0) {
            for (int i = 0; i < infoList.size(); i++) {
                BigDecimal tempRate = infoList.get(i).divide(totalDecimal, scale, BigDecimal.ROUND_HALF_UP);
                totalRate = totalRate.add(tempRate);

                if (totalRate.compareTo(BigDecimal.ONE) > 0 || i == infoList.size() - 1) {
                    resultList.add(BigDecimal.ONE.subtract(totalRate.subtract(tempRate)).doubleValue());
                } else {
                    resultList.add(tempRate.doubleValue());
                }
            }
        } else {
            resultList = infoList.stream().map(BigDecimal::doubleValue).collect(Collectors.toList());
        }
        return resultList;
    }

    /**
     * @description : 环比统计
     * @param: infoList
     * @param: scale
     * @return : java.util.List<java.lang.Double>
     * @author : kenn
     * @date : 2022/5/17 14:25
     */
    public static List<Double> ratioRatePercent(List<Long> infoList,int scale) {
        List<Double> rateList = new ArrayList<>(infoList.size() - 1);
        for (int i = 1; i < infoList.size(); i++) {
            Long beforeCount = infoList.get(i - 1);
            Long count = infoList.get(i);
            double rate = beforeCount == 0 ? 0.0 : new BigDecimal(count - beforeCount).divide(new BigDecimal(beforeCount), 2, BigDecimal.ROUND_HALF_UP).doubleValue();
            rateList.add(rate);
        }
        return rateList;
    }

}
